package com.szml.config;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RocketMQConfig {

    @Autowired
    private RocketMQProperties rocketMQProperties;

    @Bean(destroyMethod = "destroy")
    public RocketMQTemplate rocketMQTemplate() throws MQClientException {
        // 创建DefaultMQProducer并配置
        DefaultMQProducer producer = new DefaultMQProducer(
                rocketMQProperties.getProducer().getGroup()
        );
        producer.setNamesrvAddr(rocketMQProperties.getNameServer());


        // 创建RocketMQTemplate并设置producer
        RocketMQTemplate template = new RocketMQTemplate();
        template.setProducer(producer);

        return template;
    }
}